package com.lovo.ssc.service.impl;

import com.lovo.dto.ReturnOrderDto;
import com.lovo.ssc.entity.Returngoods;
import com.lovo.ssc.service.IReturngoodsService;
import com.lovo.ssc.service.ISysGoods2Service;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReturnGoodsMQService {

    @Autowired
    IReturngoodsService returnService;
    @Autowired
    ISysGoods2Service goods2Service;
    @RabbitListener(queues = MQUtil.ReturnQueueAfter)
    public void returnGoods(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, ReturnOrderDto dto)throws Exception{
        if (dto!=null){
            Returngoods goods = new Returngoods();
            goods.setName(dto.getUsername());
            goods.setState("已入库");
            goods.setAddress("四川省成都市");
            List<String> list = dto.getCommoditybatch();
            String batch = list.get(0);
            goods.setBatch(batch);
            goods.setSpecs(dto.getNorms());
            goods.setTime(dto.getReturndate());
            goods.setGoodsname(dto.getCommodityname());
            goods.setNum(dto.getCommodityNumber());
            double d = dto.getRetuenprice();
            float f = (float)d;
            goods.setPrice(f);
            returnService.save(goods);
            goods2Service.inStorageCauseByReturn
                    (dto.getCommoditycode(),batch,dto.getCommodityNumber());
        }
        channel.basicAck(tag,false); //false不进行批量确认
    }
}
